const path = require("path");
const HtmlWebpackPlugin = require("html-webpack-plugin");
const {
    CleanWebpackPlugin
} = require("clean-webpack-plugin");
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const OptimizeCSSAssetsPlugin = require("optimize-css-assets-webpack-plugin");
const TerserJSPlugin = require("terser-webpack-plugin");
const CopyPlugin = require("copy-webpack-plugin");
module.exports = {
    mode: "development",
    devtool: "inline-source-map",
    entry: {
        // 谁先声明谁先创建
        app: './src/index.js',
        //  print: './src/print.js',
    },
    // 最小化css
    optimization: {
        minimizer: [new TerserJSPlugin({}), new OptimizeCSSAssetsPlugin({})]
    },
    output: {
        // filename: 'main.js',
        // filename: 'bundle.js',
        // filename: '[name].bundle.js',
        filename: "[name].[chunkhash].bundle.js",
        path: path.resolve(__dirname, "dist")
    },
    plugins: [
        new CleanWebpackPlugin(),
        new HtmlWebpackPlugin({
            title: "Output Management",
            template: "./index.html"
        }),
        new MiniCssExtractPlugin({
            filename: "[name]-[contenthash].css"
        }),
        new CopyPlugin({
            patterns: [{
                from: "./static",
                to: "./static"
            }],
        }),
    ],
    module: {
        rules: [{
            test: /\.m?js$/,
            exclude: /node_modules/,
            use: {
                loader: "babel-loader"
            }
        },
        {
            test: /\.css$/,
            use: [MiniCssExtractPlugin.loader, "css-loader"]
        },
        {
            test: /\.(png|svg|jpg|gif|webp)$/,
            use: [{
                loader: "url-loader",
                options: {
                    limit: 10000,
                    name: "assets/[name]_[hash:10].[ext]"
                }
            }]
        },
        ]
    },
    // devServer: {
    //     contentBase: path.resolve(__dirname, "dist")
    // },
    devServer: {
        contentBase: path.resolve(__dirname, "dist"),
        port: "8080",
        proxy: {
            "/api": {
                target: "http://127.0.0.1:3001",
                pathRewrite: {
                    "^/api": ""
                }
            },
            "/resource": {
                target: "http://127.0.0.1:3002",
                pathRewrite: {
                    "^/resource": ""
                }
            }
        }
    }
};